﻿using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;

namespace FFmpegVideoClip
{
    public class Win32
    {
        [StructLayout(LayoutKind.Sequential)]
        public struct POINT
        {
            public int X;
            public int Y;

            public POINT(int x, int y)
            {
                this.X = x;
                this.Y = y;
            }
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool GetCursorPos(out POINT pt);
    }
    public class FragmentDragDropAdorner:Adorner
    {
        FrameworkElement? Element;
        public FragmentDragDropAdorner(UIElement element):base(element)
        {
            IsHitTestVisible = false;
            Element = element as FrameworkElement;
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
            if (Element != null)
            {
                if(Win32.GetCursorPos(out Win32.POINT screenPos))
                {
                    Point pos = this.PointFromScreen(new Point(screenPos.X, screenPos.Y));
                    Rect rect = new Rect(pos.X,pos.Y,Element.ActualWidth,Element.ActualHeight);
                    drawingContext.PushOpacity(1.0);
                    Brush? highlight=Element.TryFindResource(SystemColors.HighlightBrushKey) as Brush;
                    if (highlight != null)
                        drawingContext.DrawRectangle(highlight, new Pen(Brushes.Transparent, 0), rect);
                    drawingContext.DrawRectangle(new VisualBrush(Element), new Pen(Brushes.Transparent, 0), rect);
                }
            }
        }
    }
}
